package com.weitu.accidentEvent.osha.controller;

import com.weitu.accidentEvent.osha.service.FireExplosionService;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 火灾爆炸事件月报controller
 * Created by fanyuting on 2017/6/6 0006.
 */
@Controller
@RequestMapping("accidentEvent/osha/fire")
public class FireExplosionController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "accidentEvent/osha/fire/";
    @Autowired
    private FireExplosionService fireExplosionService;

    /**
     * 跳转到火灾爆炸事件列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String toFireListPage(Model model) {

        return MODULE_BATH_PATH + "list";
    }

    /**
     * 查询分页列表数据
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("queryListByParam")
    public Object queryListByParam(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = this.fireExplosionService.queryListCountByParam(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<Map<String, Object>> list = this.fireExplosionService.queryListByParam(map);
        return pagination.flushDataGridData(list);
    }

    /**
     * 跳转到火灾爆炸事件添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("add")
    public String toFireAddPage(Model model) {
        return MODULE_BATH_PATH + "add";
    }

    /**
     * 新增数据
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFireInfo")
    @Transactional
    public AjaxResult addFireInfo(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.fireExplosionService.addInfo(map);
        if (flag) {
            return success("火灾爆炸事件月报保存成功！");
        }
        return fail("火灾爆炸事件月报保存失败！");
    }

    /**
     * 跳转到编辑页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String toFireEditPage(Model model) {
        String businessId = this.getPara("businessId");
        Map<String, Object> map = this.fireExplosionService.queryInfoById(businessId);
        model.addAttribute("info", map);
        return MODULE_BATH_PATH + "edit";
    }

    /**
     * 跳转到详情页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public String toFireDetailPage(Model model) {
        String businessId = this.getPara("businessId");
        Map<String, Object> map = this.fireExplosionService.queryInfoById(businessId);
        model.addAttribute("info", map);
        return MODULE_BATH_PATH + "detail";
    }

    /**
     * 编辑数据
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editFireInfo")
    @Transactional
    public AjaxResult editFireInfo(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.fireExplosionService.editInfo(map);
        if (flag) {
            return success("火灾爆炸事件月报更新成功！");
        }
        return fail("火灾爆炸事件月报更新失败！");
    }

    /**
     * 删除数据
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteFireInfo")
    @Transactional
    public AjaxResult deleteFireInfo(HttpServletRequest request, HttpServletResponse response) {
        String businessId = this.getPara("businessId");
        boolean flag = this.fireExplosionService.deleteInfo(businessId);
        if (flag) {
            return success("火灾爆炸事件月报删除成功！");
        }
        return fail("火灾爆炸事件月报删除失败！");
    }
}
